/*
 * Implementation of checker "2.10     关键数据结构未按CacheLine对齐"
 * Author: yu wang
 * TODO: should use a smarter algorithm to detect this error instead of 
 * 1. directly identify whether the size of a struct is larger than 'cacheline_size'
 * 2. identify variables meet three conditions: commonly accessed, among critical paths, and not correctly aligned.
 */
#include <fstream>
#include <iostream>
#include <list>
#include <queue>
#include <sstream>
#include <stack>
#include <string>

#include <clang/AST/AST.h>
#include <clang/AST/ASTConsumer.h>
#include <clang/AST/Expr.h>
#include <clang/AST/RecursiveASTVisitor.h>
#include <clang/AST/RecordLayout.h>
#include <clang/Frontend/ASTConsumers.h>
#include <clang/Frontend/ASTUnit.h>
#include <clang/Tooling/Tooling.h>
#include <llvm/Support/raw_ostream.h>

#include "framework/BasicChecker.h"
#include "framework/Report.h"
#include <framework/VariableAnalyzer.h>

using namespace clang;
using namespace llvm;
using namespace clang::driver;
using namespace clang::tooling;
using namespace std;

class AlignCacheline : public BasicChecker {
public:
  AlignCacheline(ASTResource *resource, ASTManager *manager,
                  CallGraph *call_graph, Config *configure)
      : BasicChecker(resource, manager, call_graph, configure){};
  void check();

private:
  void getStucture();
  void readConfig();
  bool analyzeRWPosCheckFailed(RecordDecl* RD, const RWAccess *rwAccess);
  void analyzeBug(RecordDecl* RD, const RWAccess* rwAccess);
  int cacheline_size;
  float avg_dev_threshold;
  int frequent_access_threshold;
  int DefaultLoopCount;
};
